package com.wh.wisdomsite.xbox.server.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;

/**
 * WebUtil，用于调取接口，返回数据
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.server.util
 * @author: 丁奕
 * @date: 2018-09-11 10:27
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
public class WebUtil {
    public static String getMessageFromUrl(String url) throws IOException {
        if(url == null || url.equals("")){
            throw new IllegalArgumentException("Illegal URL:" + url);
        }
        URL url_ = new URL(url);
        URLConnection connection = url_.openConnection();
        connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        connection.setRequestProperty("charset", "utf-8");
        connection.setDoInput(true);
        connection.setConnectTimeout(40000);
        connection.setReadTimeout(40000);
        connection.setUseCaches(false);
        HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
        httpUrlConnection.connect();
        int responseCode = httpUrlConnection.getResponseCode();
        InputStream inputStream = null;
        if (responseCode == 200) {
            inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
        } else {
            inputStream = new BufferedInputStream(httpUrlConnection.getErrorStream());
        }
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String response = bufferedReader.readLine();
        String temp = "";
        while (response != null) {
            temp += response;
            response = bufferedReader.readLine();

        }
        return temp;
    }
    public static String getMessageFromUrl(String url, Map<String, String> header) throws IOException{
        if(url == null || url.equals("")){
            throw new IllegalArgumentException("Illegal URL:" + url);
        }
        URL url_ = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) url_.openConnection();
        connection.setRequestProperty("ContentType", "UTF-8");
        connection.setRequestProperty("Content-type", "text/html");
        connection.setRequestProperty("Accept-Charset", "utf-8");
        connection.setRequestProperty("contentType", "utf-8");
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
        connection.setRequestProperty("Accept","*/*");
        for (String key : header.keySet()) {
            connection.addRequestProperty(key, header.get(key));
        }
        InputStream is = null;
        if ( connection.getResponseCode() >= 400) {
            is = connection.getErrorStream();
        } else {
            is = connection.getInputStream();
        }
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String response = bufferedReader.readLine();
        String temp = "";
        while (response != null) {
            temp += response;
            response = bufferedReader.readLine();

        }
        connection.disconnect();
        return temp;
    }

    public static String postMessageToUrl(String url, Map<String, String> parameter) throws HttpException, IOException{
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        postMethod.getParams().setContentCharset("UTF-8");
        Iterator<String> names = parameter.keySet().iterator();
        while(names.hasNext()){
            String name = names.next();
            String value = parameter.get(name);
            postMethod.addParameter(name, value);
        }
        httpClient.executeMethod(postMethod);
        String response = null;
        response = postMethod.getResponseBodyAsString();
        return response;
    }

    public static String postMessageToUrl(String url, Map<String, String> parameter, Map<String, String> header) throws HttpException, IOException{
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        postMethod.getParams().setContentCharset("UTF-8");
        for (String key : header.keySet()) {
            postMethod.setRequestHeader(key, header.get(key));
        }
        Iterator<String> names = parameter.keySet().iterator();
        while(names.hasNext()){
            String name = names.next();
            String value = parameter.get(name);
            postMethod.addParameter(name, value);
        }
        httpClient.executeMethod(postMethod);
        String response = null;
        response = postMethod.getResponseBodyAsString();
        return response;
    }
}
